package prj.carmap.mobile.process;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import prj.carmap.mobile.CarmapApplication;
import prj.carmap.mobile.R;
import prj.carmap.mobile.activity.DialogView;
import prj.carmap.mobile.activity.SellerTypeActivity;
import prj.carmap.mobile.model.SellerType;
import prj.carmap.mobile.util.ViewUtil;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;

/**
 * 商家分类处理
 *
 */
public class SellerTypeProcess extends AsyncHttpResponseHandler {

	private static AsyncHttpClient client = new AsyncHttpClient();

	private Context context;

	/** 只获取数据，不切换界面 */
	private boolean fetchOnly;

	public void getSellerTypes() {
		if (CarmapApplication.sellerTypes != null)
		{
			if (!fetchOnly)
			{
				Intent intent = new Intent(context, SellerTypeActivity.class);
				//ArrayList<SellerType> sellerTypes = (ArrayList<SellerType>) CarmapApplication.sellerTypes;
				//intent.putExtra("sellerTypes", sellerTypes);
				intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				context.startActivity(intent);
			}
		}
		else
		{
			CarmapApplication.popView = new DialogView((Activity) context);
			CarmapApplication.popView.show();
			client.get(CarmapApplication.SERVER_URL + "/rest/seller/typelist", this);
		}
	}

	/**
	 * Fired when a request returns successfully, override to handle in your own
	 * code
	 *
	 * @param content
	 *            the body of the HTTP response from the server
	 * @throws JSONException
	 */
	public void onSuccess(String content) {
		try
		{
			JSONArray jsonAry = new JSONArray(content);
			Log.i("sss sellerTypes", content);
			ArrayList<SellerType> sellerTypes = new ArrayList<SellerType>();
			for (int i = 0; i < jsonAry.length(); i++)
			{
				JSONObject json = jsonAry.getJSONObject(i);
				SellerType type = new SellerType();
				type.typeId = json.getInt("typeId");
				type.typeName = json.getString("typeName");
				type.typeIcon = json.getString("typeIcon");
				JSONArray jsonArySub = json.getJSONArray("children");
				for (int j = 0; j < jsonArySub.length(); j++)
				{
					JSONObject jsonSub = jsonArySub.getJSONObject(j);
					SellerType typeS = new SellerType();
					typeS.typeId = jsonSub.getInt("typeId");
					typeS.typeName = jsonSub.getString("typeName");
					type.children.add(typeS);
				}
				sellerTypes.add(type);
			}
			CarmapApplication.sellerTypes = sellerTypes;

			if (fetchOnly)
			{
			}
			else
			{
				Intent intent = new Intent(context, SellerTypeActivity.class);
				//intent.putExtra("sellerTypes", sellerTypes);
				//intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				context.startActivity(intent);
				((Activity) context).overridePendingTransition(android.R.anim.fade_out, android.R.anim.fade_in);
			}

			CarmapApplication.popView.hide();
		}
		catch (JSONException e)
		{
			e.printStackTrace();
		}
	}

	/**
	 * Fired when a request fails to complete, override to handle in your own
	 * code
	 *
	 * @param error
	 *            the underlying cause of the failure
	 * @param content
	 *            the response body, if any
	 */
	public void onFailure(Throwable error, String content) {
		CarmapApplication.popView.hide();
		ViewUtil.makeToast(context, R.string.error_net_connect);
	}

	public void setContext(Context context) {
		this.context = context;
	}

	public boolean isFetchOnly() {
		return fetchOnly;
	}

	public void setFetchOnly(boolean fetchOnly) {
		this.fetchOnly = fetchOnly;
	}

}
